# -*- coding: utf-8 -*-
from mdMyApi import *
import thostmduserapi as MDapi
import configparser
import os




class CFtdcMdSpi(MDapi.CThostFtdcMdSpi):

    def __init__(self, mdapi):
        MDapi.CThostFtdcMdSpi.__init__(self)
        self.mdapi = mdapi
        
    def OnFrontConnected(self) -> "void":
        print("OnFrontConnected")
        self.mdapi.ctlevent.set()

    def OnFrontDisconnected(self, nTimeLapse: 'int') -> "void":
        print("OnFrontDisconnected")
        self.mdapi.ctlevent.set()

    def OnRspUserLogin(self, pRspUserLogin: 'CThostFtdcRspUserLoginField', pRspInfo: 'CThostFtdcRspInfoField', nRequestID: 'int', bIsLast: 'bool') -> "void":
        print(f"OnRspUserLogin, SessionID={pRspUserLogin.SessionID},ErrorID={pRspInfo.ErrorID},ErrorMsg={pRspInfo.ErrorMsg}")
        if(pRspInfo.ErrorID == 0):
            PrintReMsg(pRspInfo)
            self.mdapi.ctlevent.set()
        else:
            PrintReMsg(pRspInfo)
            os._exit(233)

    def OnRtnDepthMarketData(self, pDepthMarketData: 'CThostFtdcDepthMarketDataField') -> "void":
        print ("OnRtnDepthMarketData")
        mdlist=[pDepthMarketData.TradingDay,\
            pDepthMarketData.reserve1,\
            pDepthMarketData.ExchangeID,\
            pDepthMarketData.reserve2,\
            pDepthMarketData.LastPrice,\
            pDepthMarketData.PreSettlementPrice,\
            pDepthMarketData.PreClosePrice,\
            pDepthMarketData.PreOpenInterest,\
            pDepthMarketData.OpenPrice,\
            pDepthMarketData.HighestPrice,\
            pDepthMarketData.LowestPrice,\
            pDepthMarketData.Volume,\
            pDepthMarketData.Turnover,\
            pDepthMarketData.OpenInterest,\
            pDepthMarketData.ClosePrice,\
            pDepthMarketData.SettlementPrice,\
            pDepthMarketData.UpperLimitPrice,\
            pDepthMarketData.LowerLimitPrice,\
            pDepthMarketData.PreDelta,\
            pDepthMarketData.CurrDelta,\
            pDepthMarketData.UpdateTime,\
            pDepthMarketData.UpdateMillisec,\
            pDepthMarketData.BidPrice1,\
            pDepthMarketData.BidVolume1,\
            pDepthMarketData.AskPrice1,\
            pDepthMarketData.AskVolume1,\
            pDepthMarketData.BidPrice2,\
            pDepthMarketData.BidVolume2,\
            pDepthMarketData.AskPrice2,\
            pDepthMarketData.AskVolume2,\
            pDepthMarketData.BidPrice3,\
            pDepthMarketData.BidVolume3,\
            pDepthMarketData.AskPrice3,\
            pDepthMarketData.AskVolume3,\
            pDepthMarketData.BidPrice4,\
            pDepthMarketData.BidVolume4,\
            pDepthMarketData.AskPrice4,\
            pDepthMarketData.AskVolume4,\
            pDepthMarketData.BidPrice5,\
            pDepthMarketData.BidVolume5,\
            pDepthMarketData.AskPrice5,\
            pDepthMarketData.AskVolume5,\
            pDepthMarketData.AveragePrice,\
            pDepthMarketData.ActionDay,\
            pDepthMarketData.InstrumentID,\
            pDepthMarketData.ExchangeInstID,\
            pDepthMarketData.BandingUpperPrice,\
            pDepthMarketData.BandingLowerPrice]

        DealTooMax(mdlist)
        print(mdlist)

    def OnRspSubMarketData(self, pSpecificInstrument: 'CThostFtdcSpecificInstrumentField', pRspInfo: 'CThostFtdcRspInfoField', nRequestID: 'int', bIsLast: 'bool') -> "void":
        print ("OnRspSubMarketData")
        print ("InstrumentID=",pSpecificInstrument.InstrumentID)
        #print ("ErrorID=",pRspInfo.ErrorID)
        #print ("ErrorMsg=",pRspInfo.ErrorMsg)

#深度行情处理异常值
def DealTooMax(mdlist):
    maxprice = 1e+15
    minprice = -1e+15
    for i in range(len(mdlist)):
        if((type(mdlist[i]) is not str) and (mdlist[i] > maxprice or mdlist[i] < minprice)):
            mdlist[i] = 0
        else:
            mdlist[i] = mdlist[i]



#static function
#print errormsg
def PrintReMsg(pRspInfog):
    errorid = pRspInfog.ErrorID
    errmsg = pRspInfog.ErrorMsg
    remsg = "ErrorID= " + str(errorid) + " ErrorMsg= " + str(errmsg)
    print(remsg)

#print func name
def PrintReName(rename):
    remsg = "receive" + rename
    print(remsg)
